package com.kk.crowd.mvc.handler;

import com.kk.crowd.util.ResultEntity;
import com.kk.crowd.entity.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AjaxHandler {


    private Logger logger = LoggerFactory.getLogger (AjaxHandler.class);

    @RequestMapping("/test/error2.html")
    public String testArithmeEeeor() {//基于注解的算术异常测试
        int i = 1 / 0;
        return "target";
    }

    @RequestMapping("/test/error1.html")
    public String testNullPointerError() {//基于注解的空指针异常测试
        String a = null;
        System.out.println (a.length ( ));
        return "target";
    }

    @RequestMapping("/test/error0.html")
    public String testBasedXMLError() {//基于XML异常处理机制
        String a = null;
        System.out.println (a.length ( ));
        return "target";
    }

    @ResponseBody
    @RequestMapping("/send/object.json")
    public ResultEntity<Student> testObjectjson(@RequestBody Student student) {
        logger.info (student.toString ( ));
        // 封装返回ajax数据
        ResultEntity<Student> resultEntity = ResultEntity.successWithoutData (student);
        return resultEntity;
    }

    @ResponseBody
    @RequestMapping("/send/object.html")
    public String testObject(@RequestBody Student student) {
        logger.info (student.toString ( ));
        return "Success";

    }


    @ResponseBody
    @RequestMapping("/send/array/two.html")
    public String testReceiveArrayTwo(@RequestBody List<Integer> jsonArray) {
        for (Integer number : jsonArray) {
            System.out.println ("number:" + number);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("/send/array/one.html")
    public String testReceiveArrayOne(@RequestParam("array[]") List<Integer> array) {
        for (Integer number : array) {
            System.out.println ("number:" + number);
        }
        return "success";
    }
}
